/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __DIM_HASH_H
#define __DIM_HASH_H

#include <crypto/hash.h>

#define MAX_DIGEST_SIZE 64 /* sha512 algo size is the bigest one, 64 bytes */
#define MAX_HEX_SIZE 128 /* sha512 digest convert to hex format is 128 bytes */
#define MAX_HASH_DESCSIZE 512

#define DIM_SHASH_DESC_ON_STACK(shash)				  \
	char __##shash##_desc[sizeof(struct shash_desc) + \
	MAX_HASH_DESCSIZE] CRYPTO_MINALIGN_ATTR; \
	struct shash_desc *shash = (struct shash_desc *)__##shash##_desc

enum dim_hash_type {
	DIM_HASH_SHA256,
	DIM_HASH_LAST,
};

/**
 * dim_hash_get_index_by_name - get the hash index corresponding to given hash
 * algorithm name
 *
 * @name: algorithm name
 *
 * Return: number for dim_hash_type, DIM_HASH_LAST for unsupport algorithm
 */
enum dim_hash_type dim_hash_get_index_by_name(const char *name);

/**
 * dim_hash_get_name_by_index - get the hash algorithm name corresponding to
 * given hash index
 *
 * @hash_index: index for hash algorithm
 *
 * Return: string of hash algorithm name or "" for invalid index
 */
const char *dim_hash_get_name_by_index(enum dim_hash_type hash_index);

/**
 * dim_hash_get_cur_index - get the current dim global hash algorithm
 *
 * Return: number for dim_hash_type
 */
enum dim_hash_type dim_hash_get_cur_index(void);

/**
 * dim_hash_set_cur_index - set the current dim global hash algorithm
 *
 * @algo: hash algorithm name to be set
 */
void dim_hash_set_cur_index(const char *algo);

/**
 * dim_hash_get_digest_len - get digest length corresponding to given hash
 * algorithm index
 *
 * @hash_index: index for hash algorithm
 *
 * Return: number for digest length on byte
 */
int dim_hash_get_digest_len(enum dim_hash_type hash_index);

/**
 * dim_hash_algo_uninit - destroy resource of dim hash algorithm
 */
void dim_hash_algo_uninit(void);

/**
 * dim_hash_algo_init - init resource of dim hash algorithm
 */
int dim_hash_algo_init(void);

/**
 * dim_hash_algo_sys_2_dim - change the kernel hash index to dim hash index
 *
 * @algo: kernel hash index
 *
 * Return: number for dim hash index, DIM_HASH_LAST for invalid hash index
 */
enum dim_hash_type dim_hash_algo_sys_2_dim(int algo);

/**
 * dim_hash_get_tfm - get hash algorithm tfm struct by dim hash index
 *
 * @hash_index: index for hash algorithm
 *
 * Return: hash algorithm tfm, NULL for invalid hash algorithm
 */
struct crypto_shash *dim_hash_get_tfm(enum dim_hash_type hash_index);

#endif
